Load all required libraries.
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2 ✓ purrr 0.3.4
## ✓ tibble 3.0.1 ✓ dplyr 1.0.0
## ✓ tidyr 1.1.0 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Create the plot:
1)Start with a basic bar plot to show Department of Public Health Data
2)Layer a line plot to show 7-day moving average
3)Layer a scatterplot to show wastewater data
4)Layer a horizontal line and annotation to indicate the limit of detection (LOD) for RT-qPCR
5)Finish with asthetics (note legend asthetics are split to maintain theme_classic)
clean_plot <- final_data %>% ggplot() +
geom_bar(aes(x = date, y = new_cases_clarke), stat = "sum", alpha = 0.35, fill = "#7570B3", show.legend = FALSE)
clean_plot <- clean_plot +
geom_line(aes(x = date, y = X10_day_ave_clarke), size = 1, color ="#E6AB02") +
geom_point(aes(x = date, y = log_copy_per_L*10, color = target, shape = Facility), size = 2.5) +
geom_hline(yintercept = 35.5, linetype = "dashed") +
annotate("text", x = as.Date("2020-03-20"), y = 39, label = "LOD")
clean_plot <- clean_plot +
scale_y_continuous(limits = c(0,80), breaks = seq(0, 80, 20),
sec.axis = sec_axis(~. / 10, name = "SARS CoV-2 Log Copies Per L")) +
ylab("Clarke County Daily Cases") + xlab("Date") +
theme_classic() + guides(color=guide_legend("SARS CoV-2 Target"))
clean_plot <- clean_plot +
theme(legend.position = "none") #generating tidy legend as png file
clean_plot <- clean_plot + scale_color_manual(na.translate = FALSE, values = c("#1B9E77", "#D95F02")) + scale_shape_manual(na.translate = FALSE, values = c(1:2))
clean_plot
## Warning: Removed 2 rows containing non-finite values (stat_sum).
## Warning: Removed 10 row(s) containing missing values (geom_path).
## Warning: Removed 150 rows containing missing values (geom_point).
Save the plot!
ggsave(filename = "./athens_covid_wastewater_master_plot.png", plot = clean_plot)
## Saving 7 x 5 in image
## Warning: Removed 2 rows containing non-finite values (stat_sum).
## Warning: Removed 10 row(s) containing missing values (geom_path).
## Warning: Removed 150 rows containing missing values (geom_point).
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X10_day_ave_clarke, cases_per_100000_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
ten_day_ave_color <- "#E6AB02"
marker_colors <- c('N1' = '#1B9E77','N2' = '#D95F02')
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", range = c(0,80), showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as ten day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X10_day_ave_clarke,
type = "scatter",
mode = "lines",
name = "Ten Day Moving Average Athens",
colors = ten_day_ave_color)
#renders the main plot layer three as positive target hits
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~log_copy_per_L,
type = "scatter",
mode = "markers",
data = only_positives,
colors = marker_colors,
symbol = ~Facility,
yaxis = "y2") %>%
layout(yaxis2 = list(overlaying = "y", side = "right", title = "SARS CoV-2 Log Copies Per L", range = c(0, 8), showline = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
p1
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: Ignoring 1 observations
save(p1, file = "./plotly_fig.rda")
only_background_2 <- subset(only_background, date > as.Date("2020-05-20"))
p2 <- only_background_2 %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", range = c(0,80), showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as ten day moving average
p2 <- p2 %>% plotly::add_trace(x = ~date, y = ~X10_day_ave_clarke,
type = "scatter",
mode = "lines",
name = "Ten Day Moving Average Athens",
colors = ten_day_ave_color)
#renders the main plot layer three as positive target hits
p2 <- p2 %>% plotly::add_trace(x = ~date, y = ~log_copy_per_L,
type = "scatter",
mode = "markers",
data = only_positives,
colors = marker_colors,
symbol = ~Facility,
yaxis = "y2") %>%
layout(yaxis2 = list(overlaying = "y", side = "right", title = "SARS CoV-2 Log Copies Per L", range = c(0, 8), showline = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
p2
save(p2, file = "./plotly_fig_2.rda")